package com.qili.service.it.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.qili.base.mapper.BaseMapper;
import com.qili.base.service.impl.BaseServiceImpl;
import com.qili.entity.it.Tree;
import com.qili.entity.supervisor.SupervisorFile;
import com.qili.mapper.it.ItDicMapper;
import com.qili.service.it.ItDicService;
import com.qili.util.CommonUtil;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuyang
 * @date 2021/3/18
 * @description
 */
@Log
@Service
public class ItDicServiceImpl extends BaseServiceImpl<SupervisorFile,String> implements ItDicService {

    @Autowired
    private ItDicMapper itDicMapper;

    @Override
    public BaseMapper<SupervisorFile, String> getMappser() {
        return itDicMapper;
    }

    @Override
    public int insertSelective(SupervisorFile record) {
        if(StringUtils.isBlank(record.getDeleteState())){
            record.setDeleteState("1");
        }
        return super.insertSelective(record);
    }

    @Override
    public List<Tree> selectAllDic() {
        return itDicMapper.selectAllDic();
    }

    @Override
    public List<Tree> getAllDicSeted() {
        Map paraMap = new HashMap();
        paraMap.put("examine", CommonUtil.getUserRoleList());
//        List<Tree> firstForTree = itDicMapper.getFirstForTree(paraMap);
        return itDicMapper.getAllDicSeted(paraMap);
    }


    @Override
    public List<Map> selectDicRoleList(String id) {
        return itDicMapper.selectDicRoleList(id);
    }

    @Override
    public void formatDicFiles(SupervisorFile dic) {
        //查找给目录所属的文件(包括文件夹和文件)
        SupervisorFile childListSelect = new SupervisorFile();
        childListSelect.setSuperiorId(dic.getId());
        List<SupervisorFile> select = select(childListSelect);
        String parentPath = dic.getUrl();
        if(StringUtils.isNotBlank(parentPath)){
            if(CollectionUtil.isNotEmpty(select)){
                for(SupervisorFile child : select){
                    if(StringUtils.isNotBlank(child.getFileName())){
                        String childNewUrl = parentPath.endsWith(File.separator) ?
                                parentPath + child.getFileName() : parentPath + File.separator + child.getFileName();
                        child.setUrl(childNewUrl);
                        File newFile = new File(childNewUrl);
                        if(newFile.exists()){
                            SupervisorFile updateFile = new SupervisorFile();
                            updateFile.setId(child.getId());
                            updateFile.setUrl(childNewUrl);
                            if(newFile.isDirectory()){
                                if(StringUtils.isNotBlank(dic.getExamineUser())){
                                    updateFile.setExamineUser(dic.getExamineUser());
                                }
                                if(StringUtils.isNotBlank(dic.getDownloadUser())){
                                    updateFile.setExamineUser(dic.getDownloadUser());
                                }
                                if(StringUtils.isNotBlank(dic.getCanotSeeRoles())){
                                    updateFile.setExamineUser(dic.getCanotSeeRoles());
                                }
                                if(StringUtils.isNotBlank(dic.getLinkId())){
                                    updateFile.setExamineUser(dic.getLinkId());
                                }
                                updateFile.setZskfileType("1");
                            }
                            updateByPrimaryKeySelective(updateFile);
                            if(newFile.isDirectory()){
                                formatDicFiles(child);
                            }
                        }else{
                            log.warning("格式化文件夹下面所属的下级文件url时,某文件没有对应的fileName");
                        }
                    }else{
                        log.warning("格式化文件夹下面所属的下级文件url时,某文件没有对应的fileName");
                    }
                }
            }
        }
    }

    @Override
    public void addViewCount(String fileId) {
        itDicMapper.addpageViewCount(fileId);
    }

    @Override
    public void addDownloads(String fileId) {
        itDicMapper.addDownloadCount(fileId);
    }

    @Override
    public void updateChildDicPermission(SupervisorFile file) {
        if(file!=null){
            SupervisorFile childDics = new SupervisorFile();
            childDics.setZskfileType("1");
            childDics.setSuperiorId(file.getId());
            List<SupervisorFile> childDicList = select(childDics);
            if(CollectionUtil.isNotEmpty(childDicList)){
                for(SupervisorFile aChild : childDicList){
                    SupervisorFile updateFile = new SupervisorFile();
                    updateFile.setId(aChild.getId());
                    updateFile.setExamineUser(file.getExamineUser());
                    updateFile.setDownloadUser(file.getDownloadUser());
                    updateFile.setCanotSeeRoles(file.getCanotSeeRoles());
                    updateByPrimaryKeySelective(updateFile);
                    updateChildDicPermission(updateFile);
                }
            }
        }
    }
}
